package com.winphone.uvam.controller;

import com.winphone.uvam.entity.Role;
import com.winphone.uvam.entity.SysUser;
import com.winphone.uvam.service.IRoleService;
import com.winphone.uvam.service.ISysUserService;
import com.winphone.uvam.utils.PropertyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Properties;

/**
 * @author: zhou
 * @Description:
 * @Date:Create in 2017/11/10
 * @Modified By:
 */
@Controller
public class HomeController {

    @Autowired
    ISysUserService sysUserService;
    @Autowired
    IRoleService roleService;

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = {"/index","/"})
    public String index(HttpServletResponse response){
      /* Session session =  SecurityUtils.getSubject().getSession();
       SysUser sysUser = SessionUtil.getCurrentUser();
       Long sessionId = SessionCacheUtil.getLoginUserId();*/
        return "index";
    }


    @RequestMapping(value = {"/api/qqq"})
    @ResponseBody
    public String qqq(){
        Properties propertiesVersion =  PropertyUtil.readProperties();
        String kkk = propertiesVersion.getProperty("app.androidPlat");
        return kkk;
    }


    @RequestMapping("/loginUser")
    public String loginUser(String username,String password,HttpSession session,HttpServletRequest request) {
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//        token.setRememberMe(true);
        Subject subject = SecurityUtils.getSubject();
        try {
            if (!subject.isAuthenticated()){
                subject.login(token);           //完成登录
            }
            SysUser user = (SysUser) subject.getPrincipal();
            System.out.println("session"+session.getId());
            Role role = roleService.getRoleById(user.getRoleId());
            session.setAttribute("user", user);
            session.setAttribute("userRole", role);

//            SessionCacheUtil.setLoginUserId(user.getId());
            return "redirect:index";

        }catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
            return "login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("msg", "用户或密码不正确！");
            return "login";
        }

    }
    @RequestMapping("/logOut")
    public String logOut(HttpSession session) {
        session.removeAttribute("user");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    /**
     * 用户
     * @return
     */
    @RequestMapping(value = "sysUser/page",method = RequestMethod.GET)
    public String sysUser() {
        return "admin/user/list";
    }

    /**
     * 角色
     * @return
     */
    @RequestMapping(value = "role/page",method = RequestMethod.GET)
    public String role() {
        return "admin/role/list";
    }

    /**
     * 课程分类
     * @return
     */
    @RequestMapping(value = "course/type/page",method = RequestMethod.GET)
    public String courseType() {
        return "course/type/list";
    }


    /**
     * 课程列表
     * @return
     */
    @RequestMapping(value = "course/list/page",method = RequestMethod.GET)
    public String courseList() {
        return "course/list/list";
    }

    /**
     * 课程信息
     * @return
     */
    @RequestMapping(value = "course/info/page",method = RequestMethod.GET)
    public String courseInfo() {
        return "course/list/info";
    }

    /**
     * 学校列表
     * @return
     */
    @RequestMapping(value = "school/page",method = RequestMethod.GET)
    public String schoolList() {
        return "admin/school/list";
    }


    /**
     * 订单列表
     * @return
     */
    @RequestMapping(value = "order/orderlist/page",method = RequestMethod.GET)
    public String orderList() {
        return "order/orderlist/list";
    }

    /**
     * 转课
     */
    @RequestMapping(value = "order/changeCourse/page", method = RequestMethod.GET)
    public String changeCourse() {
        return "order/orderlist/changecourse";
    }

    /**
     * 优惠券
     * @return
     */
    @RequestMapping(value = "order/couponcard/page",method = RequestMethod.GET)
    public String couponcardList() {
        return "order/couponcard/list";
    }

    /**
     * 教师列表
     */
    @RequestMapping(value = "teacher/list/page",method = RequestMethod.GET)
    public String teacherList() {
        return "teacher/list/list";
    }

    /**
     * 教师信息
     */
    @RequestMapping(value = "teacher/info/page",method = RequestMethod.GET)
    public String teacherInfo() {
        return "teacher/list/info";
    }

    /**
     * 活动列表
     * @return
     */
    @RequestMapping(value = "activity/page",method = RequestMethod.GET)
    public String activityList() {
        return "admin/activity/list";
    }

    /**
     * banner列表
     * @return
     */
    @RequestMapping(value = "banner/list/page",method = RequestMethod.GET)
    public String bannerList() {
        return "banner/list/list";
    }

    /**
     * vip用户列表
     */
    @RequestMapping(value = "vip/list/page", method = RequestMethod.GET)
    public String vipUserList() {
        return "vipuser/list/list";
    }

    /**
     * 退款列表
     * @return
     */
    @RequestMapping(value = "order/refunds/page",method = RequestMethod.GET)
    public String refunds() {
        return "order/refunds/list";
    }

    /**
     * 发票列表
     * @return
     */
    @RequestMapping(value = "invoice/list/page",method = RequestMethod.GET)
    public String invoice() {
        return "invoice/list/list";
    }

    /**
     * 退款列表
     *
     * @return
     */
    @RequestMapping(value = "order/stat/page", method = RequestMethod.GET)
    public String orderStat() {
        return "order/stat/list";
    }
}
